package com.vcom.sms;

import com.cloudlead.common.worker.client.RestClientExecutor;
import com.ruanwei.interfacej.SmsSendException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.springframework.util.Assert;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Dean on 2015/5/5.
 */
public class SmsSendClient implements SmsSendApi {
    private final String loginName;

    private final String loginPwd;

    private final String url;

    private XStream stream;

    private HttpClient httpClient;

    public SmsSendClient(String loginName, String loginPwd, String url) {
        this.loginName = loginName;
        this.loginPwd = MD5.md5(loginPwd);
        this.url = url;
        httpClient = RestClientExecutor.getHttpClient();
        stream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));
    }

    @Override
    public void send(String[] mobile, String content) {
        Assert.notEmpty(mobile, "mobile is required.");
        Assert.hasText(content, "content is required.");

        SingleContentGroup group = new SingleContentGroup();
        group.setLoginName(loginName);
        group.setLoginPwd(loginPwd);
        group.setInterfaceId("-1");
        group.setSerType("sms");
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        group.setSendTime(format.format(date));

        group.setMobile(StringUtils.join(mobile, ","));
        group.setContent(content);
        send(group);
    }

    @Override
    public void send(List<Task> tasks) {
        MultiContentGroup group = new MultiContentGroup();
        group.setLoginName(loginName);
        group.setLoginPwd(loginPwd);
        group.setInterfaceId("-1");
        group.setSerType("sms");
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        group.setSendTime(format.format(date));

        group.setItems(tasks);
        send(group);
    }

    @Override
    public void sendVerificationCode(String mobile, String content) {
        MultiContentGroup group = new MultiContentGroup();
        group.setLoginName(loginName);
        group.setLoginPwd(loginPwd);
        group.setInterfaceId("-1");
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        group.setSendTime(format.format(date));
        group.setSerType("simt");

        List<Task> tasks = new ArrayList<>();
        tasks.add(new Task(mobile, content));
        group.setItems(tasks);
        send(group);
    }

    private void send(Group group) {
        HttpPost postRequest = new HttpPost(url);
        try {
            stream.processAnnotations(group.getClass());
            String xml = stream.toXML(group);
            xml = xml.replace("&lt;", "<").replace("&gt;", ">");
            System.out.println(xml);
            postRequest.setHeader("Content-Type", "text/xml;charset=gbk");

            StringEntity entity = new StringEntity(xml, "gbk");
            postRequest.setEntity(entity);

            HttpResponse httpResponse = httpClient.execute(postRequest);
            String response = EntityUtils.toString(httpResponse.getEntity());

            if (!"00".equals(response)) {
                throw new SmsSendException("Sms send error. Response is [" + response + "]");
            }

        } catch (IOException e) {
            e.printStackTrace();
            throw new SmsSendException("Sms send error", e);
        }
    }

}
